package com.fdq.bigevent.service.Imp;

import com.fdq.bigevent.mapper.UserMapper;
import com.fdq.bigevent.pojo.Result;
import com.fdq.bigevent.pojo.User;
import com.fdq.bigevent.service.UserService;
import com.fdq.bigevent.util.JwtUtil;
import com.fdq.bigevent.util.Md5Util;
import com.fdq.bigevent.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImp implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User findUserByName(String username) {
        return userMapper.findUserByName(username);
    }

    @Override
    public Result register(String username, String password) {
        //判断用户是否已经存在
        User userByName = findUserByName(username);
        if(userByName == null){
            //加密密码
            String md5String = Md5Util.getMD5String(password);
            userMapper.insert(username, md5String);
            return Result.success();
        }else {
            return Result.error("该用户名已存在");
        }
    }

    public Result<String> login(String username, String password){
        //判断用户是否已经存在
        User userByName = findUserByName(username);
        if(userByName == null){
            return Result.error("该用户不存在");
        }
        //判断用户密码是否正确
        if(Md5Util.checkPassword(password,userByName.getPassword())){
            Map<String,Object> claim = new HashMap();
            claim.put("id",userByName.getId());
            claim.put("username",userByName.getUsername());
            String token = JwtUtil.genToken(claim);
            return Result.success(token);
        }
        return Result.error("用户名或密码错误");
    }

    @Override
    public Result update(User user) {
        //更新信息更新时间
        user.setUpdateTime(LocalDateTime.now());
        if(userMapper.update(user) == 1){
            return Result.success();
        }else {
            return Result.error("用户更新失败");
        }

    }

    @Override
    public Result updateAvatar(String avatarUrl) {
        //获取当前用户id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        if(userMapper.updateAvater(avatarUrl, id)==1){
            return Result.success();
        }else {
            return Result.error("用户更新失败");
        }
    }

    @Override
    public Result updatePwd(String oldPwd, String newPwd) {
        //获取当前用户id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        String username =(String) map.get("username");
        User user = userMapper.findUserByName(username);
        if(!Md5Util.checkPassword(oldPwd,user.getPassword())){
            return Result.error("原密码输入错误");
        }
        String md5String = Md5Util.getMD5String(newPwd);
        if(userMapper.updatePwd(md5String, id) != 1){
            return Result.error("更新用户密码失败");
        }else {
            return Result.success();
        }

    }
}
